package com.example.mypositioning

import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat

class PermissionUtils {


    //需动态权限申请列表https://blog.csdn.net/aiyustudio/article/details/47979977


    /**
     * 检查权限并请求访问所有文件的权限
     * @param activity 当前的Activity
     */

    public fun verifyStoragePermissions( activity:Activity){
        var permission=ActivityCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE)
        if(permission!=PackageManager.PERMISSION_GRANTED){
            var builder =AlertDialog.Builder(activity,android.R.style.Theme_DeviceDefault_Light_Dialog_MinWidth)
            builder.setMessage("访问SD卡")
            builder.setTitle("权限说明")
            builder.setCancelable(true)
            builder.setPositiveButton("申请",object:DialogInterface.OnClickListener{
                override fun onClick(p0: DialogInterface?, p1: Int) {
                    //TODO("Not yet implemented")
                    ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),91)
                }
            })
            builder.show()
            //我们没有被授予权限，所以我们应该去请求它
            //ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),REQUEST_EXTERNAL_STORAGE)
        }
    }

    public fun verifyPositionPermissions ( activity:Activity){
        var permission=ActivityCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_COARSE_LOCATION)
        if(permission!=PackageManager.PERMISSION_GRANTED){

            var builder =AlertDialog.Builder(activity,android.R.style.Theme_DeviceDefault_Light_Dialog_MinWidth)
            builder.setMessage("位置权限")
            builder.setTitle("权限说明")
            builder.setCancelable(true)
            builder.setPositiveButton("申请",object:DialogInterface.OnClickListener{
                override fun onClick(p0: DialogInterface?, p1: Int) {
                    //TODO("Not yet implemented")
                    ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),41)
                }
            })
            builder.show()
            //我们没有被授予权限，所以我们应该去请求它 ,Manifest.permission.ACCESS_FINE_LOCATION
            //ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),REQUEST_Storage)
        }
    }
}